package com.happy.ibidding.controller;

import com.happy.ibidding.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/img")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @GetMapping("/{imageId}")
    @ResponseBody
    public void getImage(@PathVariable String imageId, HttpServletResponse response) throws Exception {
        BufferedImage image = imageService.getImage(imageId);
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

    @PostMapping("/upload")
    public String uploadImage(@RequestParam("file") MultipartFile image) throws Exception {
        return this.imageService.saveImage(image.getBytes());

    }


}